﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MBWebService.Core;
using MBWebService.Core.Classes;
using System.Collections.ObjectModel;

namespace MBWebService.Configuration
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<RemoteDevice> computers;

        public MainWindow()
        {
            InitializeComponent();

            LoadSettings();


        }

        private void LoadSettings()
        {
            AppSettings.Initialise();
            portTxt.Text = AppSettings.UserSettings.BasePort.ToString();
            logChk.IsChecked = AppSettings.UserSettings.Log;
            loadSuccessChk.IsChecked = AppSettings.UserSettings.ShowLoadSuccess;
            ffmpegSingleChannelTxt.Text = AppSettings.UserSettings.FFMPEGSingleChannel;
            ffmpegDualChannelTxt.Text = AppSettings.UserSettings.FFMPEGDualChannel;
            computers = new ObservableCollection<RemoteDevice>(AppSettings.UserSettings.RemoteComputers);
            computerLst.ItemsSource = computers;
            // computerLst.DataContext = computers;
            autoConnectChk.IsChecked = AppSettings.UserSettings.AutoConnectToServers;
            EnableSchtuff(AppSettings.UserSettings.AutoConnectToServers);
            ShowNewComputer(false);
        }

        private void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            string errors = PerformValidation();
            if (!string.IsNullOrEmpty(errors))
            {
                MessageBox.Show(errors);
                return;
            }

            UserSettings settings = new UserSettings
            {
                AutoConnectToServers = autoConnectChk.IsChecked == true,
                BasePort = int.Parse(portTxt.Text),
                Log = logChk.IsChecked == true,
                ShowLoadSuccess = loadSuccessChk.IsChecked == true,
                FFMPEGSingleChannel = ffmpegSingleChannelTxt.Text,
                FFMPEGDualChannel = ffmpegDualChannelTxt.Text,
                RemoteComputers = computers.ToList()
            };

            AppSettings.Save(settings);
        }

        private void restoreBtn_Click(object sender, RoutedEventArgs e)
        {
            AppSettings.Restore();
            LoadSettings();
        }

        private void okBtn_Click(object sender, RoutedEventArgs e)
        {
            // Add to list...
            string val = computerTxt.Text;
            string validation = ValidateRemoteComputer(val);
            if (string.IsNullOrEmpty(validation))
            {
                ShowNewComputer(false);
                computers.Add(new RemoteDevice { DnsName = val, BasePort = int.Parse(remotePortTxt.Text) });
                computerTxt.Text = string.Empty;
            }
            else
            {
                MessageBox.Show(validation);
            }
        }

        private void addServiceBtn_Click(object sender, RoutedEventArgs e)
        {
            ShowNewComputer(true);
        }

        private void autpConnectChk_Checked(object sender, RoutedEventArgs e)
        {
            EnableSchtuff(true);
        }

        private void autpConnectChk_Unchecked(object sender, RoutedEventArgs e)
        {
            EnableSchtuff(false);
        }

        #region Validation Methods

        private string ValidateRemoteComputer(string name)
        {
            string retVal = string.Empty;

            if (string.IsNullOrEmpty(name))
                retVal += "Name cannot be emtpy.\r\n";

            if (computers.Any(o => o.DnsName == name))
                retVal += "Device already exists in list, choose another.\r\n";

            retVal += ValidatePort(remotePortTxt.Text);

            return retVal;
        }

        private string PerformValidation()
        {
            return ValidatePort(portTxt.Text);
        }

        private string ValidatePort(string potentialPort)
        {
            string retVal = string.Empty;

            int port;
            if (!int.TryParse(potentialPort, out port))
                retVal += "Incorrect value for port.\r\n";

            return retVal;
        }


        #endregion

        private void EnableSchtuff(bool enable)
        {
            addServiceBtn.IsEnabled = enable;
            computerLst.IsEnabled = enable;
            computerTxt.IsEnabled = enable;
            okBtn.IsEnabled = enable;
            remotePortTxt.IsEnabled = enable;
        }

        private void ShowNewComputer(bool show)
        {
            Visibility vis = show ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden;
            okBtn.Visibility = vis;
            computerTxt.Visibility = vis;
            remotePortTxt.Visibility = vis;
            remoteNameLbl.Visibility = vis;
            remotePortLbl.Visibility = vis;

            if (show)
            {
                remotePortTxt.Text = AppSettings.UserSettings.BasePort.ToString();
            }
        }
    }
}
